package Leetcode第一期;

import java.util.ArrayList;
import java.util.Stack;

public class 从尾到头打印链表_6 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public int[] reversePrintA(ListNode head) {
        Stack<ListNode> stack=new Stack<>();
        while(head!=null){
            stack.push(head);
            head=head.next;
        }
        int i=0;
        int[] ans=new int[stack.size()];
        while (!stack.isEmpty()){
            ans[i++]=stack.pop().val;
        }
        return ans;
    }


    ArrayList<Integer> res = new ArrayList<>();
    public int[] reversePrint(ListNode head) {
        recur(head);
        int size=res.size();
        int[] result=new int[size];
        for(int i=0;i<size;i++){
            result[i]=res.get(i);
        }
        return result;
    }

    void recur(ListNode head) {
        if(head==null) {
            return;
        }
        recur(head.next);
        res.add(head.val);
    }

}
